package com.clouddot.baipai.ui.fragment;

import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.clouddot.baipai.R;
import com.clouddot.baipai.component.DaggerITableFragmentComponent;
import com.clouddot.baipai.contract.ITableFragmentContract;
import com.clouddot.baipai.http.entity.response.getCourseTable.dayInfo;
import com.clouddot.baipai.http.entity.response.getCourseTable.getCourseTableResponse;
import com.clouddot.baipai.http.entity.response.getCourseTable.table;
import com.clouddot.baipai.infrastructure.environment.Env;
import com.clouddot.baipai.module.TableFragmentModule;
import com.clouddot.baipai.ui.activity.H_UMainActivity;
import com.clouddot.baipai.ui.adapter.TableAdapter;
import com.clouddot.baipai.util.Toast.ToastUtil;

import org.wxy.sdk.base.IBasePresenter;
import org.wxy.sdk.base.impl.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * Created by wxy on 2018/8/23.
 */

public class A_TableFrgment extends BaseFragment implements ITableFragmentContract.IView {
    private Animation loadingAnim;
    private TableAdapter getTableAdapter;
    private List<table> mDataList = new ArrayList<>();

    @Inject
    ITableFragmentContract.IPresenter mPresenter;
    @BindView(R.id.RecyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.leftLoading)
    ImageView leftLoading;
    @BindView(R.id.loadingLP)
    RelativeLayout loadingLP;


    @Override
    public IBasePresenter getPresenter() {
        return mPresenter;
    }

    @Override
    protected View inflateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState) {
        return inflater.inflate(R.layout.h_fragment_table, null);
    }

    @Override
    protected void initViews() {
        DaggerITableFragmentComponent.builder().tableFragmentModule(new TableFragmentModule(this)).build().inject(this);

        mPresenter.getCourseTable(Env.getAppContext().ClassID);
    }

    @Override
    protected void initVariables() {
        loadingAnim = AnimationUtils.loadAnimation(getActivity(), R.anim.rotate);
        LinearInterpolator lir = new LinearInterpolator();
        loadingAnim.setInterpolator(lir);
        leftLoading.startAnimation(loadingAnim);
        recyclerView.addOnScrollListener(H_UMainActivity.listener);

    }

    /**
     * fragment 隐藏和显现触发的方法，第一次生成的时候不会触发。
     *  hidden 为 false --> 显现 ；为 true --> 隐藏
     * @param hidden
     */
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            loadingLP.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
            mPresenter.getCourseTable(Env.getAppContext().ClassID);
        }
    }

    @Override
    protected void loadData() {

    }

    @Override
    public void success(int flag, Object object) {
        refluesTableInfo(object);
    }

    @Override
    public void fail(int flag, String errMsg) {
        ToastUtil.showToast(getActivity(), errMsg);

    }

    private void refluesTableInfo(Object object) {
        mDataList.clear();
        getCourseTableResponse response = (getCourseTableResponse) object;
        mDataList.add(new table(0, 0, "课程表"));
        if (Env.getAppContext().banPaiType == 0) {
            H(response);
        } else {
            V(response);
        }
        loadingLP.setVisibility(View.GONE);
        recyclerView.setVisibility(View.VISIBLE);
    }

    private void V(getCourseTableResponse response) {
        int z = response.weeksNames.size();
        if(z > 0) {
            for (int s = 0; s < z; s++) {
                mDataList.add(new table(0, s + 1, response.weeksNames.get(s).getName()));
            }
        }else{
            z = 5;
            for (int s = 0; s < z; s++) {
                mDataList.add(new table(0, s + 1, ""));
            }
        }
        if(response.classTimes.size() > 0) {
            for (int i = 0; i < response.classTimes.size(); i++) {
                mDataList.add(new table(i + 1, 0, response.classTimes.get(i).getName()));
//                    + "\n" + response.classTimes.get(i).getBeginTime() +"-" +response.classTimes.get(i).getEndTime());
                for (int j = 0; j < response.weeksNames.size(); j++) {
                    dayInfo dayInfo = response.dayInfos.get(j).get(i);
                    String TeacherName = TextUtils.isEmpty(dayInfo.getTeacherName()) ? "" : " \n " + dayInfo.getTeacherName();
                    String content = TextUtils.isEmpty(dayInfo.getCourseName()) ? "" : dayInfo.getCourseName() + TeacherName;
                    mDataList.add(new table(i + 1, j + 1, content));
                }
            }
        }else{
            for (int y = 0; y < 8; y++) {
                for (int i = 0; i < 6; i++) {
                    mDataList.add(new table(y+1 ,  i, ""));
                }
            }
        }
        GridLayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 6, OrientationHelper.VERTICAL, false);
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        getTableAdapter = new TableAdapter(getActivity(), mDataList);
        recyclerView.setAdapter(getTableAdapter);

    }

    private void H(getCourseTableResponse response) {
        int z = response.weeksNames.size();
        if(z > 0) {
            for (int s = 0; s < z; s++) {
            mDataList.add(new table(0, s + 1, response.weeksNames.get(s).getName()));
        }
            for (int y = 0; y < 7 - z; y++) {
                mDataList.add(new table(0, z + 1 + y, "放假"));
            }
        }else{
            z = 7;
            for (int s = 0; s < z; s++) {
                mDataList.add(new table(0, s + 1, ""));
            }
        }
        if(response.classTimes.size() > 0) {
            for (int i = 0; i < response.classTimes.size(); i++) {
                mDataList.add(new table(i + 1, 0, response.classTimes.get(i).getName()));
//                    + "\n" + response.classTimes.get(i).getBeginTime() +"-" +response.classTimes.get(i).getEndTime());
                for (int j = 0; j < response.weeksNames.size(); j++) {
                    dayInfo dayInfo = response.dayInfos.get(j).get(i);
                    String TeacherName = TextUtils.isEmpty(dayInfo.getTeacherName()) ? "" : "(" + dayInfo.getTeacherName() + ")";
                    String content = TextUtils.isEmpty(dayInfo.getCourseName()) ? "" : dayInfo.getCourseName() + TeacherName;
                    mDataList.add(new table(i + 1, j + 1, content));
                }
                for (int y = 0; y < 7 - z; y++) {
                    mDataList.add(new table(i + 1, z + 1 + y, ""));
                }
            }
        }else{
            for (int y = 0; y < 8; y++) {
                for (int i = 0; i < 8; i++) {
                    mDataList.add(new table(y+1 ,  i, ""));
                }
            }
        }
        GridLayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 8, OrientationHelper.VERTICAL, false);
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        getTableAdapter = new TableAdapter(getActivity(), mDataList);
        recyclerView.setAdapter(getTableAdapter);
    }
}
